package fmanganiello.tests.javatests.guava;

import java.util.Date;
import java.util.concurrent.CountDownLatch;

import com.google.common.eventbus.EventBus;

public class Publisher implements Runnable {
	private EventBus eventBus;
	private CountDownLatch latch;
	private long msWaitBeforePurchase;
	
	public Publisher(final EventBus eventBus, final CountDownLatch latch, long msWaitBeforePurchase) {
		this.eventBus = eventBus;
		this.latch = latch;
		this.msWaitBeforePurchase = msWaitBeforePurchase;
	}
	
	@Override
	public void run() {
		System.out.println(Publisher.class + ": Publisher started - Thread ID: " + Thread.currentThread().getId());
		
		try {
			Thread.sleep(msWaitBeforePurchase);
			System.out.println(Publisher.class + ": We have received a purchase request");
			eventBus.post(new PurchaseEvent("BTP", 10, 100.0, new Date()));
			System.out.println(Publisher.class + ": Purchase event notified");
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		latch.countDown();
	}
}
